package com.lottery.ruok.lottrey.adapter;

import android.graphics.Color;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.activity.PrizeListActivity;
import com.lottery.ruok.lottrey.common.Common;
import com.lottery.ruok.lottrey.retrofitModule.response.LotteryStatOneResponse;

import butterknife.BindView;
import butterknife.ButterKnife;
import ui.AreaCircle;
import utils.LogUtil;
import utils.Utils;

/**
 * Created by ASUS on 2017/11/13.
 */

public class PrizeNumberAdapter_ahk3 extends BaseAdapter {
    private FragmentActivity fragmentActivity;
    private String code;
    private LotteryStatOneResponse lotteryStatOneResponses;

    public PrizeNumberAdapter_ahk3(FragmentActivity activity, LotteryStatOneResponse mLotteryStatOneResponse, String item_code) {
        this.fragmentActivity = activity;
        this.lotteryStatOneResponses = mLotteryStatOneResponse;
        this.code = item_code;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view != null) {
            holder = (ViewHolder) view.getTag();
        } else {
            view = LayoutInflater.from(fragmentActivity).inflate(R.layout.prize_number_item_ahk3, viewGroup, false);
            holder = new ViewHolder(view);
            view.setTag(holder);
        }

        holder.container.removeAllViews();
        String[] p = lotteryStatOneResponses.getIssue_list().get(i).getPrize_num().split(",");
        for (String s : p) {
            ImageView dice = new ImageView(fragmentActivity);
            switch (s) {
                case "1":
                    dice.setImageResource(R.mipmap.dice1);
                    break;
                case "2":
                    dice.setImageResource(R.mipmap.dice2);
                    break;
                case "3":
                    dice.setImageResource(R.mipmap.dice3);
                    break;
                case "4":
                    dice.setImageResource(R.mipmap.dice4);
                    break;
                case "5":
                    dice.setImageResource(R.mipmap.dice5);
                    break;
                case "6":
                    dice.setImageResource(R.mipmap.dice6);
                    break;
            }
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(Utils.dip2px(fragmentActivity, 18), Utils.dip2px(fragmentActivity, 18));
            layoutParams.rightMargin = Utils.dip2px(fragmentActivity, 6);
            dice.setLayoutParams(layoutParams);
            holder.container.addView(dice);
        }
        String[] zonghe = lotteryStatOneResponses.getIssue_list().get(i).getZonghe().split(",");
        holder.code.setText(zonghe[0]);
        holder.daxiao.setText(zonghe[1]);
        holder.danshuang.setText(zonghe[2]);
        switch (zonghe[1]) {
            case "大":
                holder.daxiao.setVisibility(View.VISIBLE);
                holder.daxiaotext.setVisibility(View.GONE);
                holder.daxiao.setBackground(Color.parseColor("#f6a06f"));
                break;
            case "小":
                holder.daxiao.setVisibility(View.VISIBLE);
                holder.daxiaotext.setVisibility(View.GONE);
                holder.daxiao.setBackground(Color.parseColor("#9076d2"));
                break;
            case "和":
                holder.daxiao.setVisibility(View.VISIBLE);
                holder.daxiaotext.setVisibility(View.GONE);
                holder.daxiao.setBackground(Color.parseColor("#cd81d4"));
                break;
            default:
                holder.daxiao.setVisibility(View.GONE);
                holder.daxiaotext.setText(zonghe[1]);
                holder.daxiaotext.setVisibility(View.VISIBLE);
                break;
        }
        switch (zonghe[2]) {
            case "单":
                holder.danshuang.setVisibility(View.VISIBLE);
                holder.danshuangtext.setVisibility(View.GONE);
                holder.danshuang.setBackground(Color.parseColor("#dbbf91"));
                break;
            case "双":
                holder.danshuang.setVisibility(View.VISIBLE);
                holder.danshuangtext.setVisibility(View.GONE);
                holder.danshuang.setBackground(Color.parseColor("#90bfe1"));
                break;
            default:
                holder.danshuang.setVisibility(View.GONE);
                holder.danshuangtext.setText(zonghe[2]);
                holder.danshuangtext.setVisibility(View.VISIBLE);
                break;
        }

        holder.yuxiaxie.setText(zonghe[3] + "   " + zonghe[4] + "   " + zonghe[5]);
        if(Common.isOfficialK3(code)){
            holder.yuxiaxie.setVisibility(View.GONE);
        }else{
            holder.yuxiaxie.setVisibility(View.VISIBLE);
        }
        holder.timeNo.setText((lotteryStatOneResponses.getIssue_list().get(i).getOpen_time().endsWith(":00  ") ? lotteryStatOneResponses.getIssue_list().get(i).getOpen_time().substring(0, ((PrizeListActivity) fragmentActivity).lotteryStatOneResponses.getIssue_list().get(i).getOpen_time().length() - 5):lotteryStatOneResponses.getIssue_list().get(i).getOpen_time()));
        holder.no.setText(lotteryStatOneResponses.getIssue_list().get(i).getIssue_no());
        return view;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public int getCount() {
        if (lotteryStatOneResponses != null) {
            return lotteryStatOneResponses.getIssue_list().size();
        }
        return 0;
    }

    static class ViewHolder {
        @BindView(R.id.time_no)
        TextView timeNo;
        @BindView(R.id.container)
        LinearLayout container;
        @BindView(R.id.daxiao)
        AreaCircle daxiao;
        @BindView(R.id.danshuang)
        AreaCircle danshuang;
        @BindView(R.id.yuxiaxie)
        TextView yuxiaxie;
        @BindView(R.id.code)
        TextView code;
        @BindView(R.id.danshuangtext)
        TextView danshuangtext;
        @BindView(R.id.daxiaotext)
        TextView daxiaotext;
        @BindView(R.id.no)
        TextView no;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}